//
//  BPCustomLabel.m
//  BeautyPicture
//
//  Created by 沈振邦 on 16/1/12.
//  Copyright © 2016年 szb. All rights reserved.
//

#import "BPCustomLabel.h"

@implementation BPCustomLabel

- (id)init
{
    self = [super init];
    if (self) {
        //单击手势
        self.lineBreakMode = NSLineBreakByWordWrapping;
        UITapGestureRecognizer *tapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTheLabel:)];
        tapOne.numberOfTapsRequired = 1;
        tapOne.numberOfTouchesRequired = 1;
        [self addGestureRecognizer:tapOne];
        self.userInteractionEnabled = YES;
        //self.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

- (void)horizontalSetUpText:(NSString *)text
{
    self.numberOfLines = 0;
    UIFont *zbfont = [UIFont fontWithName:_sbfont size:_textfontSize];
    self.size = [self withTestFont:zbfont withText:text];
    self.text = text;
}

- (void)verticalSetUpText:(NSString *)text
{
    self.numberOfLines = 0;
    UIFont *zbfont = [UIFont fontWithName:_sbfont size:_textfontSize];
    self.size = [self withTestFont2:zbfont withText:text];
    self.text = text;
}

- (void)changeLabelFont:(NSString *)fontName
{
    
}

- (CGSize)withTestFont:(UIFont *)textFont withText:(NSString *)text
{
    //NSLog(@"----%f",self.diyAreaSize.width - self.x);
    CGSize maxSize = CGSizeMake(BPScreenW, MAXFLOAT);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys: textFont, NSFontAttributeName, nil];
    CGSize textSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    return textSize;
}

- (CGSize)withTestFont2:(UIFont *)textFont withText:(NSString *)text
{
    CGSize maxSize = CGSizeMake(self.textfontSize, self.diyAreaSize.height - self.y);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys: textFont, NSFontAttributeName, nil];
    CGSize textSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    return textSize;
}

- (void)tapTheLabel:(UITapGestureRecognizer *)sender
{
    if ([self.delegate respondsToSelector:@selector(tapCustomLabel:)]) {
        [self.delegate tapCustomLabel:sender];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
